package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Autowired
    private LogService logService;

    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for(int i = 4;i > intCode.toString().length();i--){

            unitCode = "0"+unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }

    @Override
    public Map<String, Object> listInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        Map<String,Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page-1 ) * rows;
        List<Goods> goodsList = goodsDao.getGoodsInventoryList(offSet,rows,codeOrName,goodsTypeId);
        map.put("rows",goodsList);
        //map.put("total",goodsDao.getGoodsInventoryCount(codeOrName, goodsTypeId));
        map.put("total",goodsList.size());
        logService.save(new Log(Log.SELECT_ACTION,"分页查询商品库存信息"));
        return map;
    }

    @Override
    public Map<String, Object> listGoods(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        Map<String,Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page-1)*rows;
        List<Goods> goodsList = goodsDao.getGoodsList(offSet,rows,goodsName,goodsTypeId);
        map.put("rows",goodsList);
        map.put("total",goodsList.size());
        logService.save(new Log(Log.SELECT_ACTION,"分页查询所有商品信息"));
        return map;
    }

    @Override
    public void saveGoods(Goods goods, Integer goodsId) {
        //先判断商品什么时候添加，什么时候修改
        if(goodsId!=null){
            goodsDao.updateGoods(goods);
        }else {
            goodsDao.saveGoods(goods);
        }
    }

    @Override
    public void deleteGoodsById(Integer goodsId) {
            goodsDao.deleteGoodsById(goodsId);
        }

    @Override
    public Map<String, Object> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        Map<String, Object> map = new HashMap<>();
        page = page == 0 ? 1:page;
        int offSet = (page-1) * rows;
        List<Goods> goodsList = goodsDao.noInventoryQuantityList(offSet,rows,nameOrCode);
        map.put("rows",goodsList);
        map.put("total",goodsList.size());
        return map;
    }

    @Override
    public Map<String, Object> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        Map<String, Object> map = new HashMap<>();
        page = page == 0 ? 1:page;
        int offSet = (page-1) * rows;
        List<Goods> goodsList = goodsDao.hasInventoryQuantityList(offSet,rows,nameOrCode);
        map.put("rows",goodsList);
        map.put("total",goodsList.size());
        return map;
    }

    @Override
    public void saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        Goods goods = goodsDao.selectGoodsById(goodsId);
        goods.setPurchasingPrice(purchasingPrice);
        goods.setInventoryQuantity(inventoryQuantity);
        if(goods.getState()==0){
            goods.setState(1);
        }
        goodsDao.updateStock(goods);
    }

    @Override
    public void deleteStock(Integer goodsId) {
        goodsDao.deleteStock(goodsId);
    }


}
